package org.eenie.commonlib.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Eenie on 2017/7/14.
 * Email: eeniework@gmail.com
 * Des:
 */

public class ApiRequest {


    private ApiRequest(String method, Object data) {
        params = new HashMap<>();
        this.data = data;
        this.method = method;
    }

    public static ApiRequest DEFAULT(String method, Object data) {
        ApiRequest request = new ApiRequest(method, data);
        request.setApiVersion("1");
        request.setAppId("123456");
        request.setMethod(method);
        request.setData(data);
        request.setTimestamp(String.valueOf(System.currentTimeMillis()));
//        request.sign(method, data);
        return request;
    }


    public static final String PARAM_FIELD_APPID = "appId";
    public static final String PARAM_FIELD_SIGN = "sign";
    public static final String PARAM_FIELD_TIMESTAMP = "timestamp";
    public static final String PARAM_FIELD_DATA = "data";
    public static final String PARAM_FIELD_METHOD = "method";
    public static final String PARAM_FIELD_APIVERSION = "apiVersion";


    private Map<String, Object> params;
    private Object data;
    private String method;


    //强制登录
    @JSONField(serialize = false)
    private boolean forceLogin;




    private void setAppId(String appId) {
        addExtraField(PARAM_FIELD_APPID, appId);
    }


    private void setSign(String sign) {
        addExtraField(PARAM_FIELD_SIGN, sign);
    }


    private void setTimestamp(String timestamp) {
        addExtraField(PARAM_FIELD_TIMESTAMP, timestamp);
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
        addExtraField(PARAM_FIELD_DATA, data);
    }

    public void setMethod(String method) {
        addExtraField(PARAM_FIELD_METHOD, method);
    }


    private void setApiVersion(String apiVersion) {
        addExtraField(PARAM_FIELD_APIVERSION, apiVersion);
    }


//    private void sign(String method, Object data) {
//        String src = method + ApiConstants.PRIVATE_KEY + JSON.toJSONString(data);
//        String sign = MD5.compute(src);
//        setSign(sign);
//    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    public ApiRequest forceLogin() {
        forceLogin = true;
        return this;
    }

    public boolean isForceLogin() {
        return forceLogin;
    }

    public ApiRequest addExtraField(String key, Object value) {
        params.put(key, value);
        return this;
    }


    public JSONObject getDataJson() {
        return JSON.parseObject(JSON.toJSONString(data));
    }


    public ApiRequest addDataExtraField(String key, Object value) {
//        setData(getDataJson().fluentPut(key, value));
//        sign(method, data);
        return this;
    }




    public Map<String, Object> getParams() {
        return params;
    }
}
